﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySpace.Data;
public partial class Manage_News_List : System.Web.UI.Page
{
    private static int nowpages;
    protected string type="";
    private BindMethod MyMethod;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.type = string.IsNullOrEmpty(Request.QueryString["type"]) ? "" : Request.QueryString["type"];
        if (this.type.Equals("dengxin"))
        {
            MyMethod = new DengXinMethod(this);
        }
        else
        {
            MyMethod = new NewsMethod(this);
        }
        if (!IsPostBack)
        {
            nowpages = 1;
            this.MyMethod.DataBd();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (this.MyMethod.RowDeleting(((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text))
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除成功');</script>");
            this.MyMethod.DataBd();
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('删除失败');</script>");
        }

    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.MyMethod.DataBd();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.MyMethod.DataBd();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.MyMethod.DataBd();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(Label2.Text);
        this.MyMethod.DataBd();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.MyMethod.DataBd(); ;
    }
    protected void delall_Click(object sender, EventArgs e)
    {
        int i = 0;
        i = this.MyMethod.DeleteDataGroup();
        if (i == 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请选择要删除的信息');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('成功删除"+i+"条信息');</script>");
        }
        this.MyMethod.DataBd();
    }
    public interface BindMethod
    {
        //方法接口
        bool RowDeleting(string id);
        void DataBd();
        int DeleteDataGroup();
    }
    public class NewsMethod : BindMethod
    {
        GridView GridView1;
        Manage_News_List page;
        LinkButton toone;
        LinkButton toup;
        LinkButton tonext;
        LinkButton tolast;
        DropDownList jumppage;
        Label Label1;
        Label Label2;
        Label tpagesizes;
        Label pagesum;
        public NewsMethod(Manage_News_List page)
        {
            this.page = page;
            this.GridView1 = page.GridView1;
            this.toone = page.toone;
            this.toup = page.toup;
            this.tonext = page.tonext;
            this.tolast = page.tolast;
            this.jumppage = page.jumppage;
            this.Label1 = page.Label1;
            this.Label2 = page.Label2;
            this.tpagesizes = page.tpagesizes;
            this.pagesum = page.pagesum;
        }
        public bool RowDeleting(string id)
        {
            string sql = string.Format("delete from News where id={0}", id);
            DBHandler dbh = GetHandler.Init();
            try
            {
                dbh.CommandText = sql;
                dbh.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void DataBd()
        {
            DBHandler bdh = GetHandler.Init();
            string newsclass = string.Empty;
            newsclass += string.IsNullOrEmpty(page.Request.QueryString["bigclass"]) ? "" : "BigClassId=" + page.Request.QueryString["bigclass"] + " ";
            newsclass += string.IsNullOrEmpty(page.Request.QueryString["smallclass"]) ? "" : "SmallClassId=" + page.Request.QueryString["smallclass"];
            newsclass += string.IsNullOrEmpty(page.Request.QueryString["searchkeyword"]) ? "" : "NewTitle like '%" + page.Request.QueryString["searchkeyword"] + "%'";
            newsclass = string.IsNullOrEmpty(newsclass.Trim()) ? "" : "where " + newsclass;
            bdh.CommandText = string.Format("select * from news {0} order by  IsTop desc,id desc", newsclass); ;
            DataTable newstable = bdh.ExecuteDataTable();
            bdh.CommandText = "select * from News_small_type ";
            DataTable smalltable = bdh.ExecuteDataTable();
            bdh.CommandText = "select * from News_big_type";
            DataTable bigtable = bdh.ExecuteDataTable();
            var newssound =
                (from news in newstable.AsEnumerable()
                 join small in smalltable.AsEnumerable() on
                 news.Field<int>("SmallClassId") equals
                 small.Field<int>("smallclassid")
                 join big in bigtable.AsEnumerable() on
                 news.Field<int>("BigClassId") equals
                 big.Field<int>("bigclassid")
                 select new
                 {
                     id = news.Field<int>("id"),
                     NewTitle = news.Field<string>("NewTitle"),
                     AddTime = news.Field<DateTime>("AddTime").ToString("yyyy/MM/dd"),
                     BigClassId = news.Field<int>("BigClassId"),
                     SmallClassId = news.Field<int>("SmallClassId"),
                     IsTop = news.Field<int>("IsTop"),
                     Shenhe = news.Field<int>("Shenhe"),
                     smallclassname = small.Field<string>("smallclassname"),
                     bigclassname = big.Field<string>("bigclassname")
                 }).ToList();
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = newssound;
            ps.AllowPaging = true;
            ps.PageSize = 20;
            toone.Enabled = true;
            toup.Enabled = true;
            tonext.Enabled = true;
            tolast.Enabled = true;
            if (nowpages == 1)
            {
                toone.Enabled = false;
                toup.Enabled = false;
            }
            if (nowpages == ps.PageCount)
            {
                tonext.Enabled = false;
                tolast.Enabled = false;
            }
            if (!page.IsPostBack)
            {
                for (int i = 1; i <= ps.PageCount; i++)
                {
                    jumppage.Items.Add(i.ToString());
                }
            }
            Label1.Text = nowpages.ToString();
            Label2.Text = ps.PageCount.ToString();
            tpagesizes.Text = ps.PageSize.ToString();
            pagesum.Text = ps.DataSourceCount.ToString();
            ps.CurrentPageIndex = nowpages - 1;//获取当前页码
            GridView1.DataSource = ps;
            GridView1.DataBind();
        }
        public int DeleteDataGroup()
        {
            //返回删除条数
            int i = 0;
            foreach (GridViewRow gv in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)gv.FindControl("CheckBox1");
                Label lb = (Label)gv.FindControl("id");
                if (cb.Checked)
                {
                    DBHandler dbh = GetHandler.Init();
                    dbh.CommandText = string.Format("delete from News where id={0}", lb.Text);
                    dbh.ExecuteNonQuery();
                    i++;
                }
            }
            return i;
        }
    }
    public class DengXinMethod : BindMethod
    {
        GridView GridView1;
        Manage_News_List page;
        LinkButton toone;
        LinkButton toup;
        LinkButton tonext;
        LinkButton tolast;
        DropDownList jumppage;
        Label Label1;
        Label Label2;
        Label tpagesizes;
        Label pagesum;
        public DengXinMethod(Manage_News_List page)
        {
            this.page = page;
            this.GridView1 = page.GridView1;
            this.toone = page.toone;
            this.toup = page.toup;
            this.tonext = page.tonext;
            this.tolast = page.tolast;
            this.jumppage = page.jumppage;
            this.Label1 = page.Label1;
            this.Label2 = page.Label2;
            this.tpagesizes = page.tpagesizes;
            this.pagesum = page.pagesum;
        }
        public bool RowDeleting(string id)
        {
            string sql = string.Format("delete from DengXinArticle where id={0}", id);
            DBHandler dbh = GetHandler.Init();
            try
            {
                dbh.CommandText = sql;
                dbh.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void DataBd()
        {
            DBHandler bdh = GetHandler.Init();
            string newsclass = string.Empty;
            newsclass += string.IsNullOrEmpty(page.Request.QueryString["bigclass"]) ? "" : "PeriodsId=" + page.Request.QueryString["bigclass"] + " ";
            newsclass += string.IsNullOrEmpty(page.Request.QueryString["smallclass"]) ? "" : "ChapterId=" + page.Request.QueryString["smallclass"];
            newsclass += string.IsNullOrEmpty(page.Request.QueryString["searchkeyword"]) ? "" : "NewTitle like '%" + page.Request.QueryString["searchkeyword"] + "%'";
            newsclass = string.IsNullOrEmpty(newsclass.Trim()) ? "" : "where " + newsclass;
            bdh.CommandText = string.Format("select * from DengXinArticle {0} order by  IsTop desc,id desc", newsclass); ;
            DataTable newstable = bdh.ExecuteDataTable();
            bdh.CommandText = "select * from DengXinCatalogue ";
            DataTable smalltable = bdh.ExecuteDataTable();
            bdh.CommandText = "select * from DengXinBook";
            DataTable bigtable = bdh.ExecuteDataTable();
            var newssound =
                (from news in newstable.AsEnumerable()
                 join small in smalltable.AsEnumerable() on
                 news.Field<int>("ChapterId") equals
                 small.Field<int>("CatalogueId")
                 join big in bigtable.AsEnumerable() on
                 news.Field<int>("PeriodsId") equals
                 big.Field<int>("BookId")
                 select new
                 {
                     id = news.Field<int>("id"),
                     NewTitle = news.Field<string>("NewTitle"),
                     AddTime = news.Field<DateTime>("AddTime").ToString("yyyy/MM/dd"),
                     BigClassId = news.Field<int>("PeriodsId"),
                     SmallClassId = news.Field<int>("ChapterId"),
                     IsTop = news.Field<int>("IsTop"),
                     Shenhe = news.Field<int>("Shenhe"),
                     smallclassname = small.Field<string>("ChapterName"),
                     bigclassname = big.Field<string>("PeriodsName")
                 }).ToList();
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = newssound;
            ps.AllowPaging = true;
            ps.PageSize = 20;
            toone.Enabled = true;
            toup.Enabled = true;
            tonext.Enabled = true;
            tolast.Enabled = true;
            if (nowpages == 1)
            {
                toone.Enabled = false;
                toup.Enabled = false;
            }
            if (nowpages == ps.PageCount)
            {
                tonext.Enabled = false;
                tolast.Enabled = false;
            }
            if (!page.IsPostBack)
            {
                for (int i = 1; i <= ps.PageCount; i++)
                {
                    jumppage.Items.Add(i.ToString());
                }
            }
            Label1.Text = nowpages.ToString();
            Label2.Text = ps.PageCount.ToString();
            tpagesizes.Text = ps.PageSize.ToString();
            pagesum.Text = ps.DataSourceCount.ToString();
            ps.CurrentPageIndex = nowpages - 1;//获取当前页码
            GridView1.DataSource = ps;
            GridView1.DataBind();
        }
        public int DeleteDataGroup()
        {
            //返回删除条数
            int i = 0;
            foreach (GridViewRow gv in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)gv.FindControl("CheckBox1");
                Label lb = (Label)gv.FindControl("id");
                if (cb.Checked)
                {
                    DBHandler dbh = GetHandler.Init();
                    dbh.CommandText = string.Format("delete from DengXinArticle where id={0}", lb.Text);
                    dbh.ExecuteNonQuery();
                    i++;
                }
            }
            return i;
        }

    }
}